package demo.security.error;

import org.springframework.http.HttpHeaders;
import org.springframework.http.ResponseEntity;
import org.springframework.security.oauth2.common.exceptions.OAuth2Exception;
import org.springframework.security.oauth2.provider.error.WebResponseExceptionTranslator;
import org.springframework.stereotype.Component;

@Component("demoWebResponseExceptionTranslator")
public class DemoWebResponseExceptionTranslator implements WebResponseExceptionTranslator {

	@Override
	public ResponseEntity<OAuth2Exception> translate(Exception e) throws Exception {

			
		HttpHeaders headers = new HttpHeaders();
		headers.set("Cache-Control", "no-store");
		headers.set("Pragma", "no-cache");
		
		int statusCode = 500;
		if(e instanceof OAuth2Exception) {
			statusCode = ((OAuth2Exception) e).getHttpErrorCode();
		}
		
		return ResponseEntity.status(statusCode).headers(headers).body(new DemoOauth2Exception(e.getMessage()));
	}
}
